package cn.sunxyz.methodinference.rule;

import cn.sunxyz.methodinference.util.StringUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangrd on 2017/5/27.
 */
public class SQLKeywords {

    private static Map<String, Keywords> keywordsMap = new HashMap<>();

    static {
        Keywords[] keywordsArray = Keywords.values();
        for (Keywords keywords : keywordsArray) {
            keywordsMap.put(underlineToCamel(keywords), keywords);
        }
    }

    public static boolean isPresent(String key) {
        return keywordsMap.containsKey(key);
    }

    public static Keywords getKeywords(String key) {
        return keywordsMap.get(key);
    }

    private static String underlineToCamel(Keywords keywords) {
        String camel = StringUtil.underlineToCamel(keywords.name().toLowerCase());
        return camel.substring(0, 1).toUpperCase() + camel.substring(1);
    }

    //关键字的位置信息
    public enum KeywordsPosition {
        LEFT,//左边
        MIDDLE,//中间
        RIGHT//右边
    }

    //sql关键字
    //where
    public enum Keywords {
        AND(KeywordsPosition.MIDDLE), OR(KeywordsPosition.MIDDLE),
        BETWEEN(KeywordsPosition.RIGHT), LESS_THAN(KeywordsPosition.RIGHT), LESS_THAN_EQUAL(KeywordsPosition.RIGHT), GREATER_THAN(KeywordsPosition.RIGHT), GREATER_THAN_EQUAL(KeywordsPosition.RIGHT),
        IS_NULL(KeywordsPosition.RIGHT), NOT_NULL(KeywordsPosition.RIGHT), IS_NOT_NULL(KeywordsPosition.RIGHT),
        LIKE(KeywordsPosition.RIGHT), NOT_LIKE(KeywordsPosition.RIGHT),
        NOT(KeywordsPosition.RIGHT),
        IN(KeywordsPosition.RIGHT), NOT_IN(KeywordsPosition.RIGHT),
        ORDER_BY(KeywordsPosition.LEFT), DESC(KeywordsPosition.RIGHT), ASC(KeywordsPosition.RIGHT);
        private KeywordsPosition position;

        Keywords(KeywordsPosition position) {
            this.position = position;
        }

        public KeywordsPosition getPosition() {
            return position;
        }
    }


    public static void main(String[] args) {
        keywordsMap.forEach((s, keywords) -> System.out.println(s + "=>" + keywords));
    }
}
